﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using HMACAuthentication.WebApi.Filters;
using PetaPoco;
using System.Runtime.Caching;
using System.Configuration;
using YMJF_Service.Models;
using YMJF_Service.Common;


namespace YMJF_Service.Controllers
{

    /// <summary>
    ///菜单处理类
    /// </summary>
    [CompressContent]
    [HMACAuthenticationAttribute]
    [RoutePrefix("api/Menu")]
    public class MenuController : ApiController
    {
        ConnectionStringMySQLDB db = new ConnectionStringMySQLDB();
        MemoryCacher mc = new MemoryCacher();


        /// <summary>
        /// 新增菜单
        /// </summary>
        /// <param name="data">菜单实体</param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public ApiResult AddMenu(menu data)
        {
            ApiResult res = new ApiResult();
            try
            {
                string user = data.SysCreatedBy == null ? "admin" : data.SysCreatedBy;
                data.ID = Guid.NewGuid().ToString();
                data.SysCreatedBy = user;
                data.SysCreate = DateTime.Now;
                data.SysLastUpdBy = user;
                data.SysLastUpd = DateTime.Now;
                data.SysDeleted = false;
                object obj = data.Insert();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "新增菜单成功";
                }
                else
                {
                    res.code = HttpCode.Add_Failed;
                    res.message = "新增菜单失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu","新增菜单异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 删除菜单
        /// </summary>
        /// <param name="ID">菜单ID</param>
        /// <returns></returns>
        [HttpDelete]
        [Route("Del")]
        public ApiResult DelMenu(string ID)
        {
            ApiResult res = new ApiResult();
            try
            {
                string sql = string.Format("update Menu  SET SysDeleted = 1 where ID='{0}'", ID);
                int ret = db.Execute(sql);
                if (ret > 0)
                {
                    res.code = HttpCode.success;
                    res.message = "删除菜单成功";
                }
                else
                {
                    res.code = HttpCode.Del_Failed;
                    res.message = "删除菜单失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu","删除菜单异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }


        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <param name="ID">实体ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Get")]
        public ApiResult GetMenu(string ID)
        {
            ApiResult res = new ApiResult();
            try
            {
                menu data = db.FirstOrDefault<menu>("WHERE ID=@0", ID);
                if (data != null)
                {
                    res.code = HttpCode.success;
                    res.message = "获取菜单信息成功";
                    res.data = data;
                }
                else {
                    res.code = HttpCode.Que_Failed;
                    res.message = "获取菜单信息失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu", "获取菜单信息异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 选择菜单列表
        /// </summary>
        /// <param name="fid">父级菜单</param>
        /// <returns></returns>
        [HttpGet]
        [Route("Select")]
        public ApiResult SelectMenu(string fid)
        {
            ApiResult res = new ApiResult();
            try
            {
                string strwhere = "";
                if (!string.IsNullOrWhiteSpace(fid))
                {
                    strwhere += string.Format(" and FID='{0}' ", fid);
                }
                string sql = string.Format("where SysDeleted=0 {0} order by SysOrder,FID ", strwhere);
                List<menu> list = db.Fetch<menu>(sql);
                res.code = HttpCode.success;
                res.message = "选择菜单列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu", "选择菜单列表异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 选择菜单列表(一级)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("Select")]
        public ApiResult SelectMenu()
        {
            ApiResult res = new ApiResult();
            try
            {
                List<menu> list = db.Fetch<menu>("where SysDeleted=0 and FID is null order by SysOrder,FID ");
                res.code = HttpCode.success;
                res.message = "选择菜单列表(一级)成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu", "选择菜单列表(一级)异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <param name="page">当前页数</param>
        /// <param name="pagesize">每页行数</param>
        /// <param name="key">关键字(菜单名)</param>
        /// <param name="fid">父级菜单</param>
        /// <returns></returns>
        [HttpGet]
        [Route("List")]
        public ApiResult GetMenuList(long page, long pagesize, string key, string fid)
        {
            ApiResult res = new ApiResult();
            try
            {
                string strwhere = "";
                if (!string.IsNullOrWhiteSpace(fid))
                {
                    strwhere += string.Format(" and m.FID='{0}' ", fid);
                }
                if (!string.IsNullOrWhiteSpace(key))
                {
                    strwhere += string.Format(" and m.Name like'%{0}%' ", key);
                }
                string sql = string.Format(@"select m.ID,m2.Name as FID,m.Name,m.Url,m.SysOrder,m.SysLastUpd
                                            from  menu m left join menu m2 on m2.ID=m.FID and m2.SysDeleted=0
                                            where m.SysDeleted=0 {0} order by m.SysOrder,m.FID", strwhere);
                Page<menu> list = db.Page<menu>(page, pagesize, sql);
                res.code = HttpCode.success;
                res.message = "获取菜单列表成功";
                res.data = list;
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu", "获取菜单列表异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }

        /// <summary>
        /// 更新菜单
        /// </summary>
        /// <param name="data">菜单实体</param>
        /// <returns></returns>
        [HttpPut]
        [Route("Update")]
        public ApiResult UpdateMenu(menu data)
        {
            ApiResult res = new ApiResult();
            try
            {
                string user = data.SysLastUpdBy == null ? "admin" : data.SysLastUpdBy;
                data.SysLastUpdBy = user;
                data.SysLastUpd = DateTime.Now;
                data.SysDeleted = false;
                object obj = data.Update();
                if (obj != null)
                {
                    res.code = HttpCode.success;
                    res.message = "更新菜单成功";
                }
                else
                {
                    res.code = HttpCode.Mod_Failed;
                    res.message = "更新菜单失败";
                }
            }
            catch (Exception ex)
            {
                MessageBL.Insert(new MessageBL("menu", "更新菜单异常：" + ex.Message));
                res.code = HttpCode.fail;
                res.message = ex.Message;
            }
            return res;
        }
    }

}
